@extends('layouts.main')

@inject('special_offer','App\Http\Presenters\ChoicePresenter')

@section('title', '物料清单')

@section('content')
<h3>@yield('title')</h3>
<div class="btn-group" role="group">
  	<a href="{{ action('BusinessData\MaterialController@create') }}" class="btn btn-primary">添加物料</a>
  	<a href="{{ action('BusinessData\MaterialController@import') }}" class="btn btn-warning">导入数据</a>
</div>

@if(session('success'))
<div class="alert alert-success" role="alert">{{ session('success') }}</div>
@endif

<div class="clearfix"></div>
<hr>

<form class="form-inline pull-right" method="get" action="{{ action('BusinessData\MaterialController@index') }}">
    
    <div class="form-group ">
        <label for="year">年份</label>
        <div class='input-group' id='year'>
            <input type='text' name="year" class="form-control" value="{{ $request_params['year'] }}" />
        </div>
    </div>

    <div class="form-group">
        <label for="material">物料</label>
        <div class='input-group' id='material'>
            <input type='text' name="material" class="form-control" value="{{ $request_params['material'] }}" />
        </div>
    </div>

    <div class="form-group">
        <label for="category_small_id">统计用细分类</label>
        <select class="form-control" id="category_small_id" name="category_small_id">
            <option value="9999">请选择分类</option>
            <option value="0" @if($request_params['category_small_id'] == '0') selected @endif>未分类</option>
            @foreach($category_smalls as $value)
            <option value="{{ $value->id }}" @if($request_params['category_small_id'] == $value->id) selected @endif>{{ $value->name }}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="category_big_id">大分类</label>
        <select class="form-control" id="category_big_id" name="category_big_id">
            <option value="9999">请选择分类</option>
            <option value="0" @if($request_params['category_big_id'] == '0') selected @endif>未分类</option>
            @foreach($category_bigs as $value)
            <option value="{{ $value->id }}" @if($request_params['category_big_id'] == $value->id) selected @endif>{{ $value->name }}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="category_contract_id">合同大类</label>
        <select class="form-control" id="category_contract_id" name="category_contract_id">
            <option value="9999">请选择分类</option>
            <option value="0" @if($request_params['category_contract_id'] == '0') selected @endif>未分类</option>
            @foreach($category_contracts as $value)
            <option value="{{ $value->id }}" @if($request_params['category_contract_id'] == $value->id) selected @endif>{{ $value->name }}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="category_key_product_id">重点产品</label>
        <select class="form-control" id="category_key_product_id" name="category_key_product_id">
            <option value="9999">请选择分类</option>
            <option value="0" @if($request_params['category_key_product_id'] == '0') selected @endif>未分类</option>
            @foreach($category_key_products as $value)
            <option value="{{ $value->id }}" @if($request_params['category_key_product_id'] == $value->id) selected @endif>{{ $value->name }}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="is_special_offer">是否特价</label>
        <select class="form-control" id="is_special_offer" name="is_special_offer">
            <option value="9999">请选择</option>
            @foreach(config('report.special_offer') as $value)
            <option value="{{ $value }}" @if($request_params['is_special_offer'] === $value) selected @endif>{{ $special_offer->judgeWhether($value) }}</option>
            @endforeach
        </select>
    </div>

    <button type="submit" name="search" value="search" class="btn btn-primary">查询</button>
    <button type="submit" name="delete" value="delete" class="btn btn-danger">根据条件删除数据</button>

</form>

<div class="clearfix"></div>
<hr>

<table class="table table-bordered table-striped table-hover">
    <tr>
		<th>id</td>
		<th>年度</td>
		<th>物料编码</td>
		<th>物料描述</td>
		<th>统计用细分类</td>
		<th>大分类</td>
		<th>合同大类</td>
		<th>重点产品分类</td>
		<th>是否特价</td>
		<th>操作</td>
    </tr>
  @forelse($models as $model)
	<tr>
		<td>{{ $model->id }}</td>
		<td>{{ $model->year }}</td>
		<td>{{ $model->material }}</td>
		<td>{{ $model->material_desc }}</td>
		<td>{{ $model->category_small ? $model->category_small->name : ''}}</td>
		<td>{{ $model->category_big ? $model->category_big->name : ''}}</td>
		<td>{{ $model->category_contract ? $model->category_contract->name : ''}}</td>
		<td>{{ $model->category_key_product ? $model->category_key_product->name : ''}}</td>
		<td>{{ $special_offer->judgeWhether($model->is_special_offer) }}</td>
		<td>
			<a href="{{ action('BusinessData\MaterialController@edit', $model) }}" class="btn btn-warning btn-sm">编辑</a>
			<button name="delete" value="{{ $model->id }}" class="btn btn-danger btn-sm">删除</button>
		</td>
	</tr>
  @empty
  	<tr><td>暂无数据</td></tr>
  @endforelse
</table>
{{ $models->appends([
    'year' => request()->year,
    'material' => request()->material,
    'category_small_id' => request()->category_small_id,
    'category_big_id' => request()->category_big_id,
    'category_contract_id' => request()->category_contract_id,
    'category_key_product_id' => request()->category_key_product_id,
    ])->links() }}
    
<script>
      $(document).ready(function(){
        $("button[name=delete]").click(function(){
            var that = $(this);
            var id = that.attr("value");
            $.post("/business_data/material/" + id, {
                '_method': 'DELETE',
                '_token': '{{ csrf_token() }}',
            }, function(data){
                switch(data.status){
                    case 0:
                        that.parent().parent('tr').remove();
                        alert("删除信息成功！");
                        break;
                }
            });
        });


    });
</script>
@endsection